package com.wangsd.web.mqpay.service;

import com.gitee.fastmybatis.core.query.Query;
import com.gitee.fastmybatis.core.util.MyBeanUtil;
import com.wangsd.common.exception.BusinessException;
import com.wangsd.web.mqpay.entity.Passageway;
import com.wangsd.web.mqpay.mapper.PassagewayMapper;
import com.wangsd.web.sys.service.SysCompanyService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
public class PassagewayService {
    @Resource
    private PassagewayMapper mapper;
    @Autowired
    private SysCompanyService companyService;

    private Passageway selectByName(String name, Long id, String parentCode) {
        Query query = new Query();
        query.eq("name", name);
        query.eq("parent_code", parentCode);
        if (id != null) {
            query.notEq("id", id);
        }
        return mapper.getByQuery(query);
    }

    public List<Passageway> selectPage(Passageway queryObj, String loginCode) {
        Query query = new Query();
        if (StringUtils.isNotBlank(queryObj.getName())) {
            query.like("t.name", queryObj.getName());
        }
        if (queryObj.getParentCode() != null) {
            query.eq("t.parent_code", queryObj.getParentCode());
        }
        if (StringUtils.isNotBlank(loginCode)) {
            query.eq("t.parent_code", loginCode);
        }

        query.join("join sys_company t2 on t.parent_code = t2.code");
        List<String> column = Arrays.asList("t.*, t2.name as parentName");
        List<Map<String, Object>> mapList = mapper.listMap(column, query);
        List<Passageway> list = MyBeanUtil.mapListToObjList(mapList, Passageway.class);
        return list;
    }

    @Transactional
    public int saveOrUpdate(Passageway entity) {
        Passageway agent = this.selectByName(entity.getName(), entity.getId(), entity.getParentCode());
        if (agent != null) {
            throw new BusinessException("名称不能重复！");
        }

        if (entity.getId() == null) {
            mapper.saveIgnoreNull(entity);
        } else {
            mapper.updateIgnoreNull(entity);
        }

        return 1;
    }

    @Transactional
    public int delete(Long id) {
        return mapper.deleteById(id);
    }

    /**
     * 根据code查询所有能看到的通道
     *
     * @param parentCode
     * @return
     */
    public List<Passageway> selectAllPassageway(String parentCode) {
        Query query = new Query();
        if (parentCode != null) {
            query.likeRight("parent_code", parentCode);
        }
        return mapper.list(query);
    }

    public Passageway selectByPayCode(String payCode) {
        return mapper.getByColumn("pay_code", payCode);
    }

    /**
     * 根据客户id查询配置的通道
     *
     * @param customerId
     */
    public List<Passageway> selectWays(Long customerId) {
        Query query = new Query();
        query.eq("t2.customer_id", customerId);
        query.join("left join customer_passageway t2 on t2.passageway_id = t.id");
        List<String> column = Arrays.asList("t.*, t2.rate");
        List<Map<String, Object>> mapList = mapper.listMap(column, query);
        List<Passageway> list = MyBeanUtil.mapListToObjList(mapList, Passageway.class);
        return list;
    }

    /**
     * 根据客户code和支付标识查询通道(判断通道是否授权)
     * @param code
     * @param payCode
     * @return
     */
    public Passageway selectByCustomerNoAndPayCode(String code, String payCode) {
        Query query = new Query();
        query.eq("t3.code", code);
        query.eq("t.pay_code", payCode);
        query.join("left join customer_passageway t2 on t2.passageway_id = t.id");
        query.join("left join customer t3 on t2.customer_id = t3.id");
        List<String> column = Arrays.asList("t.*, t2.rate");
        List<Map<String, Object>> mapList = mapper.listMap(column, query);
        List<Passageway> list = MyBeanUtil.mapListToObjList(mapList, Passageway.class);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
}
